package com.jz;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by Hasee on 2022-03-03.
 */
@RestController
public class TestController {
    //jdeis
    //RedisTemplate
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TestMapper testMapper;

    @GetMapping("testRedis1")
    public void testRedis1(){
        redisTemplate.opsForValue().set("test",10,1000*10, TimeUnit.MILLISECONDS);
    }

    @GetMapping("queryRedis2")
    @Cacheable(key="'SUBCLASS_'+#id" , value="DISTRCT_OF_SINGLE")
    public UserVo testRedis2(Integer id){
        System.out.println("操作数据库了");
        UserVo userVoList = testMapper.queryById(id);
        return userVoList;
    }
    //假如是修改
    @GetMapping("queryRedis3")
    @CachePut(key="'SUBCLASS_'+#id" , value="DISTRCT_OF_SINGLE")
    public UserVo testRedis3(UserVo userVo){
        System.out.println("操作数据库了");
         testMapper.update(userVo);
        UserVo userVoList = testMapper.queryById(userVo.getId());
        return userVoList;
    }
    //假如是修改
    @GetMapping("queryRedis4")
    @CacheEvict(key="'SUBCLASS_'+#id" , value="DISTRCT_OF_SINGLE")
    public void queryRedis4(UserVo userVo){
        //删除语句
    }

    @GetMapping("queryRedis")
    public List<UserVo> testRedis(){
        String key = "all";
        //判断是否有
        if (!redisTemplate.hasKey(key)){
            System.out.println("操作数据库了");
            List<UserVo> userVoList = testMapper.queryUser();
            redisTemplate.opsForValue().set(key,userVoList,1000*10, TimeUnit.MILLISECONDS);
            return userVoList;
        }
        System.out.println("没有操作数据库");
        List<UserVo> userVoList =  (List<UserVo>)redisTemplate.opsForValue().get(key);
        return userVoList;
    }

    @GetMapping("queryRedisById")
    public  UserVo testRedis(Integer id){
        String key = "subclass"+id;
        //判断是否有
        UserVo userVoList =  (UserVo)redisTemplate.opsForValue().get(key);
        Boolean aBoolean = redisTemplate.hasKey(key);
        if (!aBoolean){
            synchronized (this){
                userVoList =  (UserVo)redisTemplate.opsForValue().get(key);
                Boolean aBoolean2 = redisTemplate.hasKey(key);
                if (!aBoolean2){
                    UserVo subClassVo =  testMapper.queryById(id);
                    System.out.println("通过数据库获取的数据*******************");
                    redisTemplate.opsForValue().set(key,subClassVo);
                }else {
                    System.out.println("通过缓存获取的数据============================");
                }
            }
        }
        System.out.println("通过缓存获取的数据！");
        return userVoList;
    }


    /*public  SubClassVo queryById(Integer id) {
        String key = "subclass"+id;
        //判断缓存中是否存在key值
        SubClassVo resultVo = (SubClassVo) redisTemplate.opsForValue().get(key);
        if (resultVo==null){
            synchronized (this){
                resultVo = (SubClassVo) redisTemplate.opsForValue().get(key);
                if (resultVo==null){
                    SubClassVo subClassVo =  subClassMapper.queryById(id);
                    System.out.println("通过数据库获取的数据*******************");
                    redisTemplate.opsForValue().set(key,subClassVo);
                }else {
                    System.out.println("通过缓存获取的数据============================");
                }
            }
        }else {
            System.out.println("通过缓存获取的数据============================");
        }
        return resultVo;
    }*/

    @GetMapping("updateRedis")
    public int updateRedis(UserVo user){
        String key ="subclass"+user.getId();
        UserVo userVo = new UserVo();
        userVo.setId(1);
        userVo.setName("李四");
        testMapper.update(userVo);
        //重新查一下数据
        UserVo userVo2= testMapper.queryById(1);
        redisTemplate.opsForValue().set(key,userVo2);
        return 1;
    }
    @GetMapping("deleteRedis")
    public int deleteRedis(){
        Integer id = 1;
        testMapper.deleteById(id);
        String key ="subclass"+id;
        redisTemplate.delete(key);
        return 1;
    }
}
